#include <stddef.h>
#include <stdlib.h>
#include "../constants.h"
#include "linklistlib.h"

void InitLinkedList ( LinkedList * pList ){    
	pList->pHead = NULL;
	pList->pTail = NULL;

	pList->iNodeCount = 0; 
}

int AddNode ( LinkedList * pList, void * pData ){
	LinkedListNode * pNewNode = ( LinkedListNode * ) malloc ( sizeof ( LinkedListNode ) );
	pNewNode->pData = pData;
	pNewNode->pNext = NULL;
	
	if( pList->iNodeCount==0 ){
		pList->pHead = pNewNode;
		pList->pTail = pNewNode;
	}else{
		pList->pTail->pNext = pNewNode;
		pList->pTail = pNewNode;
	}
	++ pList->iNodeCount;

	return pList->iNodeCount - 1;
}

void FreeLinkedList ( LinkedList * pList ){
	if( pList==NULL || pList->iNodeCount==0)
		return;

	LinkedListNode *pCurrNode, *pNextNode;
	pCurrNode = pList->pHead;

	while(pCurrNode!=NULL){
		pNextNode = pCurrNode->pNext;
		if(pCurrNode->pData!=NULL)
			free(pCurrNode->pData);
		free(pCurrNode);

		pCurrNode = pNextNode;
	}
}


